<?php
/**
 * Created by PhpStorm.
 * AdminUser: Administrator
 * Date: 2020/11/19
 * Time: 14:22
 */
namespace app\admin\controller;

use app\BaseController;
use app\common\model\AdminMenu;
use app\common\model\Company;
use app\common\model\OpenCard;
use app\common\model\User;
use think\exception\HttpResponseException;
use think\facade\Request;
use think\facade\View;

class AdminBase extends BaseController
{
    public $adminUser = null;
    protected $middleware = ['checkLogin'];
    public $user_id   = 0;
    public $company_id;
    public $store_id = 0;
    public $open_card_id = 0;

    public function initialize()
    {
//        parent::initialize();
//        if (empty($this->isLogin())) {
//            return $this->redirect(url("login/index"));b711e4itUMM2s4cHNiZ25mZXwyfDA 3k8psbgnfe
//        }
        $this->user_id    = User::get_admin_id();
        $this->company_id = Company::get_company_id();

        $user_id    = User::get_admin_id();
        $url        = Request::baseUrl(true);
        $module     = app('http')->getName();
        $controller = strtolower(Request::controller());
        $action     = Request::action();
        $res_type   = Request::param('res_type', 0);
        $res_name   = Request::param('res_name');
        $this->open_card_id    = Request::param('card_id', 0);
        if ($this->open_card_id) {
            session('card_id', $this->open_card_id);
        } else {
            $this->open_card_id = session('card_id');
        }

        $this->store_id = $this->open_card_id;

//        if ($module || $controller || $action != 'index') {
//            // 非访问后台首页，限制直接访问其他未授权的菜单首页
//            // @FIXME 限制不住其他未授权的菜单首页外的url直接访问
//            if (!$controller || $controller == 'index') {
//                $t_controller = '';
//            } else {
//                $t_controller = $controller;
//            }
//            if (!$action || $action == 'index') {
//                $t_action = '';
//            } else {
//                $t_action = $action;
//            }
//            $filter = array('module'=>$module, 'controller'=>$t_controller, 'action'=>$t_action, 'extra_res'=>$extra_res);
//            $menu_id = _uri('admin_menu', $filter, 'id');
//            if ($menu_id && !$this->check_menu_auth($menu_id)) {
//                throw new \Exception('您没有权限访问', 'error', url('controller'));
//            }
//        }

        $sidebar_menu_list    = array();
        $sidebar_menu_fathers = AdminMenu::where(['parent_id' => 0])->order("view_order")->select()->toArray();

        foreach ($sidebar_menu_fathers as $k=>$v) {
            $admin_menu_children = AdminMenu::where(['parent_id' => $v['id']])->order("view_order")->select()->toArray();

            $admin_menu_children_new = array();
            foreach ($admin_menu_children as $k1=>$v1) {
                // if (!$this->check_auth($v1['module'], $v1['controller'], $v1['action'])) {
                if ($this->check_menu_auth($v1['id']) == false) {
                    //@TODO continue;
                    continue;
                }

                // 是否选中
                $curr = 'curr';
                if ($v1['module']) {
                    if ($v1['module'] != $module) {
                        $curr = '';
                    }
                    $admin_res_url = $admin_res_add_url = $v1['module'];
                } else {
                    $admin_res_url = $admin_res_add_url = "";
                }

                if ($v1['controller']) {
//                    if ($v1['controller'] != $controller) {
//                        $curr = '';
//                    }
                    $admin_res_url .= "/".$v1['controller'];
                    $admin_res_add_url .= "/".$v1['controller'];
                }
//                if ($v1['action']) {
//                    if ($v1['action'] != $action) {
//                        $curr = '';
//                    }
                    $admin_res_url .= "/".$v1['action'];
//                }
                if ($v1['add_title']) {
                    if ($v1['add_action']) {
                        $admin_res_add_url .= "/".$v1['add_action'];
                    } else {
                        $admin_res_add_url .= "/add";
                    }
                }
                if ($v1['res_name']) {
                    if ($v1['module']) {
                        if ($v1['res_name'] != $res_name) {
                            $curr = '';
                        }
                        $admin_res_url .= "?res_name=".$v1['res_name'];
                        $admin_res_add_url .= "?res_name=".$v1['res_name'];
                    } else {
                        if ($v1['res_name'] != $res_type) {
                            $curr = '';
                        }
                        $admin_res_url .= "?res_type=".$v1['res_name'];
                        $admin_res_add_url .= "?res_type=".$v1['res_name'];
                    }
                } else {
                    if (($res_name && $v1['res_name'] != $res_name) || ($res_type && $v1['res_name'] != $res_type)) {
                        $curr = '';
                    }
                }
                $admin_menu_children_new[$k1] = $admin_menu_children[$k1];
                $admin_menu_children_new[$k1]['url'] = url($admin_res_url);
                $admin_menu_children_new[$k1]['action_url'] = url($admin_res_add_url);
                $admin_menu_children_new[$k1]['curr'] = $curr;
            }
            if ($admin_menu_children_new) {
                $sidebar_menu_list[$k] = $sidebar_menu_fathers[$k];
                $sidebar_menu_list[$k]['children'] = $admin_menu_children_new;
            }
        }

        if (!$this->open_card_id && !session('card_id') && $module != 'open_card') {
            return $this->redirect('/open_card/admin/index');
        }

        if ($this->open_card_id) {
            $open_card_info = OpenCard::get_open_card_by_id($this->open_card_id);
        }

        View::assign([
            'module'            => $module,
            'controller'        => $controller,
            'action'            => $action,
            'user_id'           => $user_id,
            'sidebar_menu_list' => $sidebar_menu_list,
            'open_card_info'    => $open_card_info,
            'store_id'          => $this->store_id,
        ]);

    }

    public function isLogin()
    {
        $this->adminUser = session('admin_id');
        if (empty($this->adminUser)) {
            return false;
        }

        return true;
    }

    public function redirect(...$args)
    {
        throw new HttpResponseException(redirect(...$args));
    }

    /**
     * 判断是否有后台menu显示的权限
     * @param 后台菜单的id $admin_menu_id
     * @return bool
     */
    private function check_menu_auth($admin_menu_id)
    {
        $admin_id = User::get_admin_id();
        $role_ids = User::get_user_role_ids($admin_id);
        foreach ($role_ids as $v) {
            $privilege = _uri('role', ['id'=>$v], 'privilege');
            // 超级管理员
            if ($privilege['privilege'] == '*') {
                return true;
            }
            $privileges = explode(',', $privilege['privilege']);
            if (in_array($admin_menu_id, $privileges)) {
                return true;
            }
        }
        return false;
    }
}